package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import classes.ConnectionDB;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.FlowLayout;
import net.miginfocom.swing.MigLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.inet.editor.BaseEditor;

import javax.swing.JPasswordField;

public class LoginPage extends JFrame {

	private JFrame frame;
	private JTextField userField;
	private JPasswordField passwordField;
	private final JButton signUp = new JButton("sign up");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {					
					LoginPage window = new LoginPage();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws IOException 
	 */
	public LoginPage() throws IOException {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws IOException 
	 */
	private void initialize() throws IOException {
		// register JWebEngine for HTML content
		JEditorPane.registerEditorKitForContentType( "text/html", "com.inet.html.InetHtmlEditorKit" );
		BaseEditor editor = new BaseEditor(true);
		editor.setVisible(true);
		
		frame = this;
		final Properties langFile = new Properties();
		try{
			FileInputStream in = new FileInputStream("C:\\Documents and Settings\\sneha\\workspace\\examSys\\src\\gui\\lang.properties");
			langFile.load(in);
		}
		catch(Exception e){
			System.out.println("file not found");
		}
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel loginPanel = new JPanel();
		loginPanel.setBackground(Color.BLACK);
		frame.getContentPane().add(loginPanel, BorderLayout.CENTER);
		loginPanel.setLayout(new GridLayout(0, 1, 0, 0));
		
		JLabel logo = new JLabel("LOGIN");
		logo.setVerticalAlignment(SwingConstants.TOP);
		logo.setFont(new Font("Tahoma", Font.BOLD, 40));
		logo.setHorizontalAlignment(SwingConstants.CENTER);
		logo.setForeground(Color.WHITE);
		loginPanel.add(logo);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.BLACK);
		loginPanel.add(panel);
		panel.setLayout(new MigLayout("", "[64px][46px,grow][grow]", "[14px][][][][][][][][][][][][][]"));
		
		JLabel userNameText = new JLabel(langFile.getProperty("usr.name"));
		userNameText.setFont(new Font("Tahoma", Font.BOLD, 23));
		userNameText.setForeground(Color.WHITE);
		panel.add(userNameText, "cell 0 1,alignx trailing");
		
		userField = new JTextField();
		panel.add(userField, "cell 1 1,grow");
		userField.setColumns(10);
		
		JLabel passText = new JLabel(langFile.getProperty("usr.password"));
		panel.add(passText, "cell 0 11,alignx left,aligny top");
		passText.setHorizontalAlignment(SwingConstants.RIGHT);
		passText.setForeground(Color.WHITE);
		passText.setFont(new Font("Tahoma", Font.BOLD, 23));
		
		JButton btnLogin = new JButton(langFile.getProperty("usr.login"));
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
		        try {
		        	boolean flag = false;
		            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		            String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
		                "databaseName=DbQA;user=sa;password=123456;";
		            Connection con = DriverManager.getConnection(connectionUrl);
		            Statement st = con.createStatement();  
		                ResultSet rec = st.executeQuery("SELECT users_name, users_password FROM USERS");  
		            while (rec.next()) {  
		                String userName = userField.getText();  
		                String pass = passwordField.getText();  
		                  
		                if (userName.equals(rec.getString("users_name"))) {   
		                    if (pass.equals(rec.getString("users_password"))) {  
		                        System.out.println("Logged in!");  
		                  
		                      new MainMenu(null,userName).setVisible(true);
		                      frame.setVisible(false);
		                      flag = true;	                      
		                    }
		                }
		            }
		                   if(!flag)
		                   {
		                        System.out.println("Check your inserted details!");	                        
		                        // generating a dialog box
		                        JFrame frame = new JFrame("JOptionPane showMessageDialog example");
		                         JOptionPane.showMessageDialog(frame,langFile.getProperty("usr.log.failed"));
		                    }  		              		      		         
		      
		            st.close();   
		             } catch (SQLException e) {
		                System.out.println("SQL Exception: "+ e.toString());
		            } catch (ClassNotFoundException cE) {
		                System.out.println("Class Not Found Exception: "+ cE.toString());
		            }
			}
		});
		
		passwordField = new JPasswordField();
		panel.add(passwordField, "cell 1 11,growx");
		panel.add(btnLogin, "flowx,cell 2 13,alignx center,aligny center");
		signUp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new addUser().setVisible(true);
              //  frame.setVisible(false);
			}
		});
		panel.add(signUp, "cell 2 13");
	}

}
